Use PIMS to load video and sequential images

pims provides three classes for loading video.

  • ImageSequence reads images from a directory.
  • Video reads standard video files (AVI, MOV, etc.).
  • TiffStack reads multi-frame TIF / TIFF files.

Once loaded, these objects can be handled alike. In software terms, each is a subclass of a generic Frames object. The differences between the formats are handled quietly by pims.

Load sequential images from a directory.

Take ImageSequence as an example. We have a folder of images here:


In [1]:
ls image_sequence


T76S3F00001.png  T76S3F00003.png  T76S3F00005.png
T76S3F00002.png  T76S3F00004.png

We can load them into an ImageSequence object.


In [2]:
import pims

In [6]:
v = pims.ImageSequence('/home/dallan/mr/mr/tests/video/image_sequence/')

We can see basic properties.


In [7]:
v


Out[7]:
<Frames>
Source File: /home/dallan/mr/mr/tests/video/image_sequence/
Frame Dimensions: 424 x 640
Cursor at Frame 0 of 5

We can print the first frame (it's an array of brightness values) or view those values as an image.


In [8]:
v[0]


Out[8]:
array([[134, 133, 133, ..., 135, 136, 134],
       [137, 136, 137, ..., 135, 134, 133],
       [135, 133, 137, ..., 134, 132, 130],
       ..., 
       [130, 129, 130, ..., 133, 133, 133],
       [129, 128, 129, ..., 131, 130, 130],
       [129, 128, 129, ..., 129, 130, 131]], dtype=uint8)

In [11]:
%matplotlib inline
from pylab import *

imshow(v[0], cmap=cm.gray)


Out[11]:
<matplotlib.image.AxesImage at 0xc0a9a0c>

Because the developer does most of his work in brightfield, all of the contructors (Video, ImageSequence, TiffStack) invert black and white when they load the video. To suppress this, set invert=False.


In [12]:
uninverted = pims.ImageSequence('image_sequence/', invert=False)
imshow(uninverted[0], cmap=cm.gray)


Out[12]:
<matplotlib.image.AxesImage at 0xc33d6ac>

Use subsections of the loaded frames.

We can select a subset of the frames for viewing or processing. Examples:

  • v[3] frame three (an array)
  • v[:10] first 10 frames (a list of arrays)
  • v[2:5] frames 2-5 including 2 and 5 (a list of arrays)
  • v[100:] frames 100 to the end (a list of arrays)

Loop through the frames to do your image processing.


In [ ]:
for frame in v[:]:
    # Do something with frame, a numpy array.

Load video files or multi-frame TIFFs.

ImageSequence relies only on numpy and scipy, which are required dependencies of mr, so it works out of the box. Video needs OpenCV, which includes the Python module cv2. TiffStack needs libtiff.

Once these dependencies are in place, Video and TiffStack work in the same way as ImageSequence.


In [14]:
v = pims.Video('/home/dallan/mr/mr/tests/water/bulk-water.mov')
# This file is not included in PIMS, to keep the file size small.
# Try it with a video file of your own.

In [15]:
v


Out[15]:
<Frames>
Source File: /home/dallan/mr/mr/tests/water/bulk-water.mov
Frame Dimensions: 640 x 424
Cursor at Frame 0 of 480

In [19]:
v = pims.TiffStack('tiff_stack.tif')

In [20]:
v


Out[20]:
<Frames>
Source File: tiff_stack.tif
Frame Dimensions: 512 x 512
Cursor at Frame 0 of 5

Extra tools

If OpenCV is installed, you can use `mr.play(v)to play back frames loaded from any format as video. You can slow playback by specifying a delay in miliseconds, likemr.play(v, wait=100)``.

If OpenCV is not installed, you can still view individual frames using imshow(v[frame_number]).